package conf

import (
	"encoding/json"
	"flag"
	"path/filepath"

	"tike.bid/commons/utils"
)

type Db struct {
	Host   string
	Port   int
	Db     string
	User   string
	Passwd string
}

type ResourceConfig struct {
	U16888Refresh int64 // 毫秒单位
	ISN99Refresh  int64 // 毫秒单位
	PS3838Refresh int64 // 毫秒单位
	U16888Tick    int64 // 毫秒单位 点水间隔
	ISNTick       int64 // 毫秒单位 点水间隔
	PS3838Tick    int64 // 毫秒单位 点水间隔
	PhantomJsBin  string
	HtmlCookieJs  string
	MaxbetJs      string
	ISNRenderJs   string
	VerifyCodeUrl string
	KafkaGroup    string
	GameDataTopic string
	HandicapTypes []int // 打水配置
	IPs           []string
	Mongo         map[string]*Db
	Redis         map[string]*Db
	Elastic       map[string]*Db
	Kafka         map[string]*Db
}

var mode string

func init() {
	flag.StringVar(&mode, "mode", "dev", "配置模式")
}

var resourceConfig *ResourceConfig

func LoadResource() ResourceConfig {
	if resourceConfig != nil {
		return *resourceConfig
	}

	curDir := utils.CurDir()
	f := "resource.json"
	if mode == "dev" {
		f = "json/" + f
	} else {
		f = "json_" + mode + "/" + f
	}

	f = filepath.Join(curDir, f)
	s, err := utils.ExtendFile(f)
	if err != nil {
		panic(err)
	}

	resourceConfig = new(ResourceConfig)
	err = json.Unmarshal([]byte(s), resourceConfig)
	if err != nil {
		panic(err)
	}
	return *resourceConfig
}
